package com.lxg.controller.firstController;

import com.lxg.dao.UserDao;
import com.lxg.pojo.User;
import com.lxg.service.firstService.LoginService;
import com.lxg.service.firstService.RegisterService;
import com.lxg.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class RegisterController {

    @Autowired
    RegisterService registerService;
    @Autowired
    LoginService loginService;
    @Autowired
    UserDao userDao;

    @RequestMapping(value = "/registSuccess",method = RequestMethod.POST)
    public String registSuccess(HttpServletRequest req, HttpSession session) throws Exception {
//        获取session域中的验证码,获取后删除验证码保证安全性

        String token = (String)session.getAttribute("verifyCode");
        session.removeAttribute("verifyCode");
        //1、获取请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sno = req.getParameter("sno");
        String email = req.getParameter("email");
        String code = req.getParameter("code");

        //给参数注入值
        User user = WebUtils.copyParamToBean(req.getParameterMap(), new User());

        if(token != null && token.equalsIgnoreCase(code)) {
//            根据数据库查询判断用户名或者邮箱或者手机号是否被占用
            User user1 = registerService.getUserByUsername(username);


            User user2 = registerService.getUserBySno(sno);
            User user3 = registerService.getUserByEmail(email);

//            判断想要注册的信息在数据库中存不存在，若存在则不行
            if (user1!= null) {
                // 把回显信息，保存到Request域中
                req.setAttribute("regist_msg", "用户名已存在！！");
                req.setAttribute("username", username);
                req.setAttribute("email", email);
                req.setAttribute("sno", sno);
                return "/first/login";
            } else if (user2 != null) {
                // 把回显信息，保存到Request域中
                req.setAttribute("regist_msg", "学号已存在！！");
                req.setAttribute("username", username);
                req.setAttribute("email", email);
                req.setAttribute("sno", sno);

                session.setAttribute("flag1","true");
                return "/first/login";
            } else if (user3 != null) {
                req.setAttribute("regist_msg","邮箱已注册！！");
                req.setAttribute("username", username);
                req.setAttribute("email", email);
                req.setAttribute("sno", sno);
                session.setAttribute("flag1","true");
                return "/first/login";
            } else {
                //可用
                //调用Sservice保存到数据库
                registerService.ReaderRegister(username, password, null, sno,
                        null, email, null);
                User loginUser = userDao.getUser(username,password);
                session.setAttribute("name", username);
                session.setAttribute("user", loginUser);
                //跳到主页面
                return "index";
            }
        }else {
            // 把回显信息，保存到Request域中
            req.setAttribute("regist_msg", "验证码错误！！");
            req.setAttribute("username", username);
            req.setAttribute("email", email);
            req.setAttribute("sno", sno);
            System.out.println("验证码[" + code + "]错误");

            session.setAttribute("flag1","true");
            return "/first/login";
        }
    }
}
